package giaodien;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import java.awt.Color;

import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;

import java.awt.Font;

import javax.swing.SwingConstants;
import javax.swing.ImageIcon;

import java.awt.Component;

import javax.swing.JScrollPane;
import javax.swing.JTable;

import controller.NhaCCController;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.NumberFormat;
import java.util.List;
import java.util.Vector;

public class NhaCC extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTable table;
	private int id;
	 java.util.List<model.NhaCC> list;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					NhaCC frame = new NhaCC("");
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @param user 
	 */
	public NhaCC(final String user) {
		setTitle("Nhà cung cấp");
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 666, 367);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "T\u00ECm ki\u1EBFm", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(75, 0, 130)));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Qu\u1EA3n l\u00FD nh\u00E0 cung c\u1EA5p", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(75, 0, 130)));
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Danh s\u00E1ch nh\u00E0 cung c\u1EA5p", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(75, 0, 130)));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(panel_2, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(panel, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 498, GroupLayout.PREFERRED_SIZE))))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
						.addComponent(panel_1, Alignment.LEADING, 0, 0, Short.MAX_VALUE)
						.addComponent(panel, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 74, Short.MAX_VALUE))
					.addGap(18)
					.addComponent(panel_2, GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE))
		);
	list = NhaCCController
				.getDSNhaCC();

		final Vector<Vector> rowData = new Vector<Vector>();
		final Vector<String> name = new Vector<String>();
		name.addElement("STT");
		name.addElement("Mã nhà cung cấp ");
		name.addElement("Tên nhà cung cấp");
		name.addElement("Địa chỉ");
		name.addElement("Email");
		name.addElement("Số điện thoại");
		Vector v;
		NumberFormat n = NumberFormat.getInstance();
		for (int i = 0; i < list.size(); i++) {
			v = new Vector();
			v.add(i + 1);
			v.add(list.get(i).getMaNCC());
			v.add(list.get(i).getTenNCC());
			v.add(list.get(i).getDiaChi());
			v.add((list.get(i).getEmail()));
			v.add((list.get(i).getSdt()));
			rowData.add(v);
		}
		System.out.println(list.size());
		
		JScrollPane scrollPane = new JScrollPane(table);
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addComponent(scrollPane, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 618, Short.MAX_VALUE)
		);
		gl_panel_2.setVerticalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE))
		);
		
		table = new JTable(rowData, name);
		scrollPane.setViewportView(table);
		panel_2.setLayout(gl_panel_2);
		
		JButton btnThm = new JButton("Thêm");
		btnThm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ThemNhaCC t = new ThemNhaCC();
				t.setVisible(true);
			}
		});
		btnThm.setForeground(new Color(139, 0, 0));
		btnThm.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnThm.setIcon(new ImageIcon(NhaCC.class.getResource("/icon/Add-item-icon.png")));
		
		JButton btnSa = new JButton("Sửa");
		btnSa.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (table.getSelectedRow() < 0) {
					JOptionPane.showMessageDialog(null,
							"Vui lòng chọn nhà cung cấp");
				} else {
					int row = table.getSelectedRow();
					int ma = list.get(row).getMaNCC();
					SuaNhaCC a = new SuaNhaCC(ma);
					a.setVisible(true);
				}
			}
			
		});
		btnSa.setForeground(new Color(139, 0, 0));
		btnSa.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnSa.setIcon(new ImageIcon(NhaCC.class.getResource("/icon/edit-icon.png")));
		
		JButton btnXa = new JButton("Xóa");
		btnXa.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (table.getSelectedRow() < 0) {
					JOptionPane.showMessageDialog(null,
							"Vui lòng chọn nhà cung cấp");
				} else {
					id = list.get(table.getSelectedRow()).getMaNCC();
					XoaNhaCC a = new XoaNhaCC(id);
					a.setVisible(true);
				}
			}
				
		});
		btnXa.setForeground(new Color(139, 0, 0));
		btnXa.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnXa.setIcon(new ImageIcon(NhaCC.class.getResource("/icon/close-icon.png")));
		
		JButton btnRefesh = new JButton("Refesh");
		btnRefesh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
				NhaCC n = new NhaCC(user);
				n.setVisible(true);
			}
		});
		btnRefesh.setForeground(new Color(139, 0, 0));
		btnRefesh.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnRefesh.setIcon(new ImageIcon(NhaCC.class.getResource("/icon/Button-Refresh-icon.png")));
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addComponent(btnThm)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnSa)
					.addGap(10)
					.addComponent(btnXa)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnRefesh)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addComponent(btnSa, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnThm, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnXa, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnRefesh, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 36, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_panel_1.linkSize(SwingConstants.HORIZONTAL, new Component[] {btnThm, btnSa, btnXa, btnRefesh});
		panel_1.setLayout(gl_panel_1);
		
		textField = new JTextField();
		textField.setColumns(10);
		
		JButton btnTm = 
				new JButton("Tìm");
		btnTm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String na = textField.getText();
				 list = NhaCCController.getNhaCC1(na);
				if (list.size() >= 1) {
					Vector<Vector> rowData_1 = new Vector<Vector>();
					Vector v1;
					for (int i = 0; i < list.size(); i++) {
						v1 = new Vector<>();
						v1.add(i + 1);
						v1.add(list.get(i).getMaNCC());
						v1.add(list.get(i).getTenNCC());
						v1.add(list.get(i).getDiaChi());
						v1.add(list.get(i).getEmail());
						v1.add(list.get(i).getSdt());
						rowData_1.add(v1);
					}
					table.setModel(new DefaultTableModel(rowData_1, name));
				} else {
					JOptionPane.showMessageDialog(null,
							"Không tìm thấy vui lòng kiểm tra lại!");
					textField.setText("");
				}
			
			}
		});
		btnTm.setHorizontalAlignment(SwingConstants.LEADING);
		btnTm.setForeground(new Color(139, 0, 0));
		btnTm.setFont(new Font("Tahoma", Font.BOLD, 12));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(20)
							.addComponent(btnTm))
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, 95, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(27, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnTm)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		contentPane.setLayout(gl_contentPane);
	}
}
